package cn.edu.zafu.bigdata.hzq;

import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CLIENT1 {
    public static void main(String[] args) {
        String host = "192.168.16.40"; // 服务端的IP地址
        int port = 12345; // 服务端监听的端口

        try (DatagramSocket socket = new DatagramSocket()) {
            String message = "TIME_REQUEST"; // 请求时间的消息
            byte[] sendData = message.getBytes();
            InetAddress serverAddress = InetAddress.getByName(host);
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, port);
            socket.send(sendPacket);

            // 接收服务端响应
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            socket.receive(receivePacket);

            // 将响应转换为字符串
            String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("Server time: " + response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}